{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.722067Z",
     "start_time": "2022-03-30T13:16:35.754640Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch \n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.737027Z",
     "start_time": "2022-03-30T13:16:36.723065Z"
    }
   },
   "outputs": [],
   "source": [
    "data = pd.read_csv('./dataset/Income1.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.767945Z",
     "start_time": "2022-03-30T13:16:36.739023Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>Education</th>\n",
       "      <th>Income</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>26.658839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>10.401338</td>\n",
       "      <td>27.306435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>10.842809</td>\n",
       "      <td>22.132410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>11.244147</td>\n",
       "      <td>21.169841</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>11.645485</td>\n",
       "      <td>15.192634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>12.086957</td>\n",
       "      <td>26.398951</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>12.488294</td>\n",
       "      <td>17.435307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>12.889632</td>\n",
       "      <td>25.507885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>13.290970</td>\n",
       "      <td>36.884595</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>13.732441</td>\n",
       "      <td>39.666109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>14.133779</td>\n",
       "      <td>34.396281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>14.535117</td>\n",
       "      <td>41.497994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13</td>\n",
       "      <td>14.976589</td>\n",
       "      <td>44.981575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14</td>\n",
       "      <td>15.377926</td>\n",
       "      <td>47.039595</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15</td>\n",
       "      <td>15.779264</td>\n",
       "      <td>48.252578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16</td>\n",
       "      <td>16.220736</td>\n",
       "      <td>57.034251</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17</td>\n",
       "      <td>16.622074</td>\n",
       "      <td>51.490919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18</td>\n",
       "      <td>17.023411</td>\n",
       "      <td>61.336621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19</td>\n",
       "      <td>17.464883</td>\n",
       "      <td>57.581988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20</td>\n",
       "      <td>17.866221</td>\n",
       "      <td>68.553714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>21</td>\n",
       "      <td>18.267559</td>\n",
       "      <td>64.310925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>22</td>\n",
       "      <td>18.709030</td>\n",
       "      <td>68.959009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>23</td>\n",
       "      <td>19.110368</td>\n",
       "      <td>74.614639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24</td>\n",
       "      <td>19.511706</td>\n",
       "      <td>71.867195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>25</td>\n",
       "      <td>19.913043</td>\n",
       "      <td>76.098135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>26</td>\n",
       "      <td>20.354515</td>\n",
       "      <td>75.775218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27</td>\n",
       "      <td>20.755853</td>\n",
       "      <td>72.486055</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>28</td>\n",
       "      <td>21.157191</td>\n",
       "      <td>77.355021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>29</td>\n",
       "      <td>21.598662</td>\n",
       "      <td>72.118790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>30</td>\n",
       "      <td>22.000000</td>\n",
       "      <td>80.260571</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Unnamed: 0  Education     Income\n",
       "0            1  10.000000  26.658839\n",
       "1            2  10.401338  27.306435\n",
       "2            3  10.842809  22.132410\n",
       "3            4  11.244147  21.169841\n",
       "4            5  11.645485  15.192634\n",
       "5            6  12.086957  26.398951\n",
       "6            7  12.488294  17.435307\n",
       "7            8  12.889632  25.507885\n",
       "8            9  13.290970  36.884595\n",
       "9           10  13.732441  39.666109\n",
       "10          11  14.133779  34.396281\n",
       "11          12  14.535117  41.497994\n",
       "12          13  14.976589  44.981575\n",
       "13          14  15.377926  47.039595\n",
       "14          15  15.779264  48.252578\n",
       "15          16  16.220736  57.034251\n",
       "16          17  16.622074  51.490919\n",
       "17          18  17.023411  61.336621\n",
       "18          19  17.464883  57.581988\n",
       "19          20  17.866221  68.553714\n",
       "20          21  18.267559  64.310925\n",
       "21          22  18.709030  68.959009\n",
       "22          23  19.110368  74.614639\n",
       "23          24  19.511706  71.867195\n",
       "24          25  19.913043  76.098135\n",
       "25          26  20.354515  75.775218\n",
       "26          27  20.755853  72.486055\n",
       "27          28  21.157191  77.355021\n",
       "28          29  21.598662  72.118790\n",
       "29          30  22.000000  80.260571"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.892122Z",
     "start_time": "2022-03-30T13:16:36.769940Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Income')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEGCAYAAABiq/5QAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAAXq0lEQVR4nO3de7BdZXnH8e9PLuWAyCEQYzghPREkjBpJ4EixUYtchLZK0gylOFZjyzTV8YJIkaCOaFuHIFaqvdkoHaIyXI2BQqvFcKlMR+hJwv0WBKIcAgmaWMSIBJ/+sdfBk51zWftkv3vttdbvM3Pm7LX27VmE/ez3POt9n6WIwMzM6uNlRQdgZmad5cRvZlYzTvxmZjXjxG9mVjNO/GZmNbN70QHkceCBB0Z/f3/RYZiZlcqaNWueiYipzftLkfj7+/sZHBwsOgwzs1KRtGG0/S71mJnVjBO/mVnNOPGbmdWME7+ZWc048ZuZ1UwpZvWYmdXJqnVDXPTdh3hy6zYO6u3hnJNms3BeX9teP+mIX9JZku6TdK+kyyXtJWmWpNslPSLpSkl7pozBzKxMVq0b4ryV9zC0dRsBDG3dxnkr72HVuqG2vUeyxC+pD/gIMBARrwd2A04HLgQujohDgS3AGaliMDMrm4u++xDbXnhxh33bXniRi777UNveI3WNf3egR9LuwN7ARuA44Jrs/hXAwsQxmJmVxpNbt7W0fzKSJf6IGAK+APyIRsL/GbAG2BoR27OHPQGMWriStETSoKTBzZs3pwrTzKyrHNTb09L+yUhZ6tkfWADMAg4C9gFOzvv8iFgeEQMRMTB16k6tJszMKumck2bTs8duO+zr2WM3zjlpdtveI+WsnhOAxyJiM4CklcB8oFfS7tmofwbQvjMWZmYlNzx7J+WsnpSJ/0fAMZL2BrYBxwODwM3AqcAVwGLg2oQxmJmVzsJ5f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AVOKjnMXjuUR4MfAndnPV4qOczLH0XT/48CBRce5C/8mewLfBO4F1gLHFR3nLhzLm4E1wF00astHFR1njuN4M40yzt0jPhd/UMbP/TjH0vbPvVs2mJnVTB1KPWZmNoITv5lZzTjxm5nVjBO/mVnNOPGbmdWME79VmqQXs26Twz87NeuSdKyk69v8vsdK+t0R2++X9N52vofZZO1edABmiW2LRrfJTjsW+DnwPwAR8ZUCYjAblUf8VkuSTs56nK8FFo3Y/xlJfzVi+96sYRaS3pv1RL9L0jeyfe/MGrStk/Q9SdOyx78fOCv7K+MtI193RN/74f7q+2f7b5F0oaQ7JD0s6S0d+w9iteLEb1XX01Tq+RNJewFfBd4JHAVM2CdI0uuAT9FYmXsEcGZ2123AMRExD7gC+HhEPA58Bbg4IuZGxPebXu7rwLnR6K9+D3D+iPt2j4ijaXTKPB+zBFzqsarbqdQjaS7wWESsz7a/CSyZ4HWOA66OiGcAImK4l/0M4MqsEdiewGPjvYik/YDeiLg127WCRkO0YcONudYA/RPEZDYpHvGb7Wg7O34u9prg8f8A/GNEzAH+MsfjJ/J89vtFPDCzRJz4rY4eBPolHZJtv2vEfY8DRwJIOpJGC2mAm4A/lnRAdt+UbP9+wFB2e/FvXoZngX2b3zgifgZsGVG/fw9wa/PjzFJy4reqa67xL4uIX9Io7dyQndwd2av9W8AUSfcBH6JxjVMi4j7gc8Ctku4Chtvmfga4WtIadmzH/O/AHw2f3G2KaTGNK13dDcwF/rqNx2s2IXfnNDOrGY/4zcxqxonfzKxmnPjNzGrGid/MrGac+M3MasaJ38ysZpz4zcxq5v8BuvnWrscejzUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(data.Education, data.Income)\n",
    "plt.xlabel('Education')\n",
    "plt.ylabel('Income')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.907080Z",
     "start_time": "2022-03-30T13:16:36.893118Z"
    }
   },
   "outputs": [],
   "source": [
    "# wx + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:34:03.050154Z",
     "start_time": "2022-03-30T13:34:03.034197Z"
    }
   },
   "outputs": [],
   "source": [
    "# 分解写法\n",
    "w = torch.randn(1, requires_grad=True)\n",
    "b = torch.zeros(1, requires_grad=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.937000Z",
     "start_time": "2022-03-30T13:16:36.923037Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-1.9555], requires_grad=True)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:36:23.520102Z",
     "start_time": "2022-03-30T13:36:23.505633Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.0780823881882688"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.951960Z",
     "start_time": "2022-03-30T13:16:36.938996Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.966920Z",
     "start_time": "2022-03-30T13:16:36.952958Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-1.9555])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:36.981880Z",
     "start_time": "2022-03-30T13:16:36.967918Z"
    }
   },
   "outputs": [],
   "source": [
    "learning_rate = 0.001\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:32:27.124723Z",
     "start_time": "2022-03-30T13:32:27.113752Z"
    }
   },
   "outputs": [],
   "source": [
    "X = torch.from_numpy(data.Education.values.reshape(-1, 1)).type(torch.FloatTensor)\n",
    "Y = torch.from_numpy(data.Income.values).type(torch.FloatTensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:34:13.633571Z",
     "start_time": "2022-03-30T13:34:13.617108Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([30, 1])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:35:54.075747Z",
     "start_time": "2022-03-30T13:35:54.065775Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:35:12.625071Z",
     "start_time": "2022-03-30T13:35:12.617093Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1])\n",
      "torch.Size([])\n",
      "torch.Size([1])\n"
     ]
    }
   ],
   "source": [
    "for x, y in zip(X, Y):\n",
    "    print(x.shape)\n",
    "    print(y.shape)\n",
    "    y_pred = torch.matmul(x, w) + b\n",
    "    print(y_pred.shape)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:16:37.011800Z",
     "start_time": "2022-03-30T13:16:36.998836Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:37:22.142124Z",
     "start_time": "2022-03-30T13:36:58.712796Z"
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# 定义训练过程\n",
    "for epoch in range(5000):\n",
    "    for x, y in zip(X, Y):\n",
    "        y_pred = torch.matmul(x, w) + b\n",
    "        # 损失函数\n",
    "        loss = (y - y_pred).pow(2).sum()\n",
    "        \n",
    "        # pytorch对一个变量多次求导, 求导的结果会累加起来. \n",
    "        if w.grad is not None:\n",
    "            # 重置w的导数\n",
    "            w.grad.data.zero_()\n",
    "        if b.grad is not None:\n",
    "            b.grad.data.zero_()\n",
    "            \n",
    "        # 反向传播, 即求w,b的导数\n",
    "        loss.backward()\n",
    "        \n",
    "        # 更新w,b\n",
    "        with torch.no_grad():\n",
    "            \n",
    "            w.data -= w.grad.data * learning_rate\n",
    "            b.data -= b.grad.data * learning_rate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:37:25.836322Z",
     "start_time": "2022-03-30T13:37:25.825351Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([5.1266], requires_grad=True)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:37:28.003549Z",
     "start_time": "2022-03-30T13:37:27.993575Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-32.6962], requires_grad=True)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:18:23.281497Z",
     "start_time": "2022-03-30T13:18:23.152841Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x2137b28a2b0>]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAAjtElEQVR4nO3de3xU1dX/8c8SsILagkgpl3KxUlRULk+kXqqtWLzwq5Cij9RqG6sWBRQFRUAeASlqFBFEBcR6QcRSRQzeg4BgUdQmRuReUIQSooCQUgW57t8fe2JDTMhkMjNnzuT7fr14ZeZkLusAs7Kzztp7m3MOEREJn8OCDkBERGKjBC4iElJK4CIiIaUELiISUkrgIiIhVTuZb3bssce6Vq1aJfMtRURCLz8/f6tzrlHZ40lN4K1atSIvLy+ZbykiEnpmtr684yqhiIiElBK4iEhIKYGLiISUEriISEgpgYuIhFRSu1BERGqSnIJCxuSuZlPxLprWr8ugC9qS2bFZ3F4/qhG4mQ0ws+VmtszM/mpmR5hZazN738zWmtnfzOzwuEUlIhJyO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c861AeZF7ofBU3z3XN4ETnbOnQr8Exia7KBi8BTfPQ/M7MfA+cCGZAdUDU9R5lzM7FygB9DeOdcOuD+AuGLxFN/9d7kPuNM51wEYHrmf6vYBtzjnTgJOB/qZ2UmE83Nf0bnE/XOfkgncOfc2sK3M4R7A1MjtqUBmMmOKVXnn4pyb45zbF7n7HtA86YFVUQX/JgDjgNuA0FwNr+Bc+gDZzrndkcdsTnpgMajgXBzw/cjtHwCbkhpUDJxzRc65DyO3/wOsBJoRws99ReeSiM99SibwCjR2zhVFbn8ONA4ymDi6Gng96CBiYWY9gELn3JKgY4mDnwJnm9n7ZrbQzE4LOqBquBkYY2b/wv8mEYbf8L5lZq2AjsD7hPxzX+ZcSovL5z5MCfxbzvc+hmbEVxEzG4b/dWt60LFUlZnVA27H/4qeDmoDx+B/5R0EPGdmFmxIMesDDHDO/RgYADwecDxRM7OjgBeAm51zO0p/L2yf+4rOJZ6f+zAl8C/MrAlA5GsofsWtiJldBfwauMKFsxn/J0BrYImZfYb/dfBDM/tRoFHFbiMwy3kfAAfwCxCFURYwK3L7eSDlL2ICmFkdfMKb7pwriT+Un/sKziXun/swJfCX8P8xiXydHWAs1WJmF+Lrxt2dczuDjicWzrmlzrkfOudaOeda4RNgJ+fc5wGHFqsc4FwAM/spcDjhXQVvE/CLyO0uwJoAY4lK5Ledx4GVzrkHSn0rdJ/7is4lIZ9751zK/QH+ChQBe/GJ4RqgIf4q9BpgLnBM0HFW41zWAv8CPor8mRx0nLGcR5nvfwYcG3Sc1fg3ORx4BlgGfAh0CTrOapzLz4F8YAm+9vo/QccZxXn8HF8e+bjU56JbGD/3hziXuH/uNZVeRCSkwlRCERGRUpTARURCSglcRCSklMBFREJKCVxEJKSUwEVEQkoJXEQkpP4/0SwR2RCoNOEAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(data.Education, data.Income)\n",
    "plt.plot(X.numpy(), (torch.matmul(X, w) + b).data.numpy(), c='r')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pytorch实现线性回归--封装写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:22:40.197914Z",
     "start_time": "2022-03-30T13:22:40.185946Z"
    }
   },
   "outputs": [],
   "source": [
    "from torch import nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:32:33.496853Z",
     "start_time": "2022-03-30T13:32:33.480896Z"
    }
   },
   "outputs": [],
   "source": [
    "# 和tensorflow中的Dense一个意思\n",
    "# wx + b\n",
    "model = nn.Linear(1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:32:33.683355Z",
     "start_time": "2022-03-30T13:32:33.665403Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Linear(in_features=1, out_features=1, bias=True)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:32:33.854896Z",
     "start_time": "2022-03-30T13:32:33.849910Z"
    }
   },
   "outputs": [],
   "source": [
    "# 定义损失函数\n",
    "loss_fn = nn.MSELoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:32:34.038419Z",
     "start_time": "2022-03-30T13:32:34.025946Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object Module.parameters at 0x000002137B336E08>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 即W\n",
    "model.parameters()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:32:34.220932Z",
     "start_time": "2022-03-30T13:32:34.203978Z"
    }
   },
   "outputs": [],
   "source": [
    "# 定义优化器\n",
    "# 优化器的第一个参数必须是要更新的模型中的参数\n",
    "opt = torch.optim.SGD(model.parameters(), lr=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:33:06.063909Z",
     "start_time": "2022-03-30T13:32:34.674725Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\2005\\.venv\\lib\\site-packages\\torch\\nn\\modules\\loss.py:528: UserWarning: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.\n",
      "  return F.mse_loss(input, target, reduction=self.reduction)\n"
     ]
    }
   ],
   "source": [
    "# 训练\n",
    "for epoch in range(5000):\n",
    "    for x, y in zip(X, Y):\n",
    "        y_pred = model(x)\n",
    "        loss = loss_fn(y, y_pred)\n",
    "        # 梯度清零操作\n",
    "        opt.zero_grad()\n",
    "        loss.backward()\n",
    "        # 更新操作\n",
    "        opt.step()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:33:37.894409Z",
     "start_time": "2022-03-30T13:33:37.876458Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Parameter containing:\n",
       "tensor([[5.1267]], requires_grad=True)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# w\n",
    "model.weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-30T13:33:41.497324Z",
     "start_time": "2022-03-30T13:33:41.481367Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Parameter containing:\n",
       "tensor([-32.6986], requires_grad=True)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# b\n",
    "model.bias"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
